package saobracaj;

import java.awt.Color;
import java.awt.Component;
import java.util.Random;

import javax.swing.JLabel;

public class Automobil extends Component implements Runnable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Thread nit = new Thread(this);
	private int posX, posY;
	private Color c;
	private int brRaskrsnice;
	private Raskrsnica raskrsnica;
	private char smer;
	private int brzina;
	private Object b = new Object();
	private JLabel label;
	
	private static int IDuk = 0;
	private int id;
	
	public Automobil(int x, int y, int br, Raskrsnica rask, int v) {
		id = IDuk++;
		Random generator = new Random();
		int i = generator.nextInt(4);
		switch (i){
		case 0: c = Color.pink;
				break;
		case 1: c = Color.blue;
				break;
		case 2: c = Color.green;
				break;
		case 3: c = Color.orange;
				break;
		}
		posX = x;
		posY = y;
		label = new JLabel();
		label.setSize(20, 20);
		label.setLocation(posX, posY);
		label.setIcon(new javax.swing.ImageIcon(
				getClass()
				.getResource(
						"/images/car.png")));
		
	
		brRaskrsnice = br;
		raskrsnica = rask;
		this.smer = smer;
		brzina = v;
		nit.start();
		//update();
		RaskrsnicaGUI.dohvatiRaskrsnicu().getJLayeredPane().add(label);
	
	}
	
	/*public void paint()
	{
		Graphics g2 = RaskrsnicaGUI.dohvatiRaskrsnicu().getJPanel().getGraphics();
		g2.
		g2.setColor(c);
		
		g2.fillOval(posX, posY, 20, 20);
		
	}
	
	public void repaint(){
		super.repaint();
		//paint();
		
	}
	
	public void update()
	{
		paint();
	}*/
	public void dolazakDoRaskrsnice(){
		switch(brRaskrsnice){
		case 11: case 12: case 13: kretanje(posX, posY + 80);
				break;
		case 21: case 22: case 23: kretanje(posX - 80, posY);		
				break;
		case 31: case 32: case 33: kretanje(posX, posY - 78);		
				break;
		case 41: case 42: case 43: kretanje(posX + 77, posY);
				break;
		}
	}
	public void kretanje(int x, int y){
		int n = (posX - x) / 3;
		if (((n * 3) < (posX - x)) && (n > 0)) n = n + 1;
		if (((n * 3) > (posX - x)) && (n < 0)) n = n - 1; 
		if (n < 0) n = -n;
		int m = (posY - y) / 3;
		if (((m * 3) < (posX - x)) && (m > 0)) m = m + 1;
		if (((m * 3) > (posX - x)) && (m < 0)) m = m - 1; 
		if (m < 0) m = -m;
		if (n < m) n = m;
		
		for(int i = 0; i < n; i++)
		{
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
				}
			if(posY < y) posY+=3;
			if(posY > y) posY-=3;
			if(posX < x) posX+=3;
			if(posX > x) posX-=3;
			label.setLocation(posX, posY);
			//label.repaint();
			}
	}
	public void prelazak(){
		synchronized (b) {
			b.notify();
			System.out.println("prodji");
		}
	}
	public void voznjaPrekoUlice(){
		switch(brRaskrsnice){
		case 11: kretanje(posX, 173);
				 kretanje(posX - 5, posY + 5);
				 kretanje(posX - 250, posY);
				 break;
		case 12: kretanje(posX, posY + 450 +  21*raskrsnica.getSeverP().indexOf(this));
				 break;
		case 13: kretanje(posX, 200);
		 		 kretanje(posX + 45, posY + 45);
		 		 kretanje(posX + 250, posY);
		 		 break;
		case 21: kretanje(325, posY);
		 		 kretanje(posX - 5, posY - 5);
		 		 kretanje(posX, posY - 250);
		 		 break;
		case 22: kretanje(posX - 450 -  21*raskrsnica.getIstokP().indexOf(this), posY);
			     break;
		case 23: kretanje(290, posY);
	 	 		 kretanje(posX - 45, posY + 45);
	 	 		 kretanje(posX, posY  + 250);
		 	 	 break;
		case 31: kretanje(posX, 298);
		 		 kretanje(posX + 5, posY - 5);
		 		 kretanje(posX + 250, posY);
		 		 break;
		case 32: kretanje(posX, posY  - 450 -  21*raskrsnica.getIstokP().indexOf(this));
				 break;
		case 33: kretanje(posX, 267);
		 		 kretanje(posX - 45, posY - 45);
		 		 kretanje(posX - 250, posY);
		 		 break;
		case 41: kretanje(188, posY);
		 	 	 kretanje(posX + 5, posY + 5);
		 	 	 kretanje(posX, posY + 250);
 		 		 break;
		case 42: kretanje(posX  + 450 +  21*raskrsnica.getIstokP().indexOf(this), posY);
				 break;
		case 43: kretanje(226, posY);
		 		 kretanje(posX + 45, posY - 45);
		 		 kretanje(posX, posY  - 250);
				 break;
		}
	}
	
	public void run() {
		synchronized(b){
			try{
				
				dolazakDoRaskrsnice();
				switch(brRaskrsnice){
					case 11: if((raskrsnica.getSeverS()[0].dohvatiSvetlo() != 3) && (raskrsnica.getSeverS()[0].dohvatiSvetlo() != 0)) {
							 raskrsnica.dodajAuto(this, 11);//TODO videti da li je ok br
							 raskrsnica.severDsenz = true;
							 b.wait();
							 
							 System.out.println("prosao sam "+id);	
							}
							synchronized (raskrsnica.kseverD) {
								raskrsnica.kseverD--;	
							}
							voznjaPrekoUlice();
							synchronized (raskrsnica.kseverD) {
								//if (raskrsnica.kseverD == 0) raskrsnica.severDsenz = false;	
							}
							label.hide();
						break;
					case 12: if ((raskrsnica.getSeverS()[1].dohvatiSvetlo() != 3) && (raskrsnica.getSeverS()[1].dohvatiSvetlo() != 0)) {
								 raskrsnica.dodajAuto(this, 12);//TODO videti da li je ok br
								 raskrsnica.severPsenz = true;
								 b.wait();
								 
								 System.out.println("prosao sam "+id);	
								
							 }		
							synchronized (raskrsnica.kseverP) {
								raskrsnica.kseverP--;
								
							}
							 voznjaPrekoUlice();
							 synchronized (raskrsnica.kseverP) {
								 //if (raskrsnica.kseverP == 0) raskrsnica.severPsenz = false;
							 }
							 label.hide();
						break;
					case 13:  if ((raskrsnica.getSeverS()[2].dohvatiSvetlo() != 3) && (raskrsnica.getSeverS()[2].dohvatiSvetlo() != 0)) {
						 raskrsnica.dodajAuto(this, 13);//TODO videti da li je ok br
						 raskrsnica.severLsenz = true;
						 b.wait();
						 
						 System.out.println("prosao sam "+id);	
						
					 }		
					synchronized (raskrsnica.kseverL) {
						raskrsnica.kseverL--;
						
					}
					 voznjaPrekoUlice();
					 synchronized (raskrsnica.kseverL) {
						// if (raskrsnica.kseverL == 0) raskrsnica.severLsenz = false;
					 }
					 label.hide();
						break;
					case 21: if ((raskrsnica.getIstokS()[2].dohvatiSvetlo() != 3) && (raskrsnica.getIstokS()[2].dohvatiSvetlo() != 0)) {
						 raskrsnica.dodajAuto(this, 21);//TODO videti da li je ok br
						 raskrsnica.istokDsenz = true;
						 b.wait();
						 
						 System.out.println("prosao sam "+id);	
						}
						synchronized (raskrsnica.kistokD) {
							raskrsnica.kistokD--;
							
						}
						voznjaPrekoUlice();
						synchronized (raskrsnica.kistokD) {
							//if (raskrsnica.kistokD == 0) raskrsnica.istokDsenz = false;
						}
						label.hide();
						break;
					case 22: if ((raskrsnica.getIstokS()[1].dohvatiSvetlo() != 3) && (raskrsnica.getIstokS()[1].dohvatiSvetlo() != 0)) {
						 raskrsnica.dodajAuto(this, 22);//TODO videti da li je ok br
						 raskrsnica.istokPsenz = true;
						 b.wait();
						 
						 System.out.println("prosao sam "+id);	
						
						}		
					synchronized (raskrsnica.kistokP) {
						raskrsnica.kistokP--;
						
					}
					 voznjaPrekoUlice();
					 synchronized (raskrsnica.kistokP) {
						 //if (raskrsnica.kistokP == 0) raskrsnica.istokPsenz = false;
					 }
					 label.hide();
						break;
					case 23: if ((raskrsnica.getIstokS()[0].dohvatiSvetlo() != 3) && (raskrsnica.getIstokS()[0].dohvatiSvetlo() != 0)) {
						 raskrsnica.dodajAuto(this, 23);//TODO videti da li je ok br
						 raskrsnica.istokLsenz = true;
						 b.wait();
						 
						 System.out.println("prosao sam "+id);	
						
						}		
					synchronized (raskrsnica.kistokL) {
						raskrsnica.kistokL--;
						
					}
					 voznjaPrekoUlice();
					 synchronized (raskrsnica.kistokL) {
						// if (raskrsnica.kistokL == 0) raskrsnica.istokLsenz = false;
					 }
					 label.hide();
						break;
					case 31: if ((raskrsnica.getJugS()[2].dohvatiSvetlo() != 3) && (raskrsnica.getJugS()[2].dohvatiSvetlo() != 0)) {
						 raskrsnica.dodajAuto(this, 31);//TODO videti da li je ok br
						 raskrsnica.jugDsenz = true;
						 b.wait();
						 
						 System.out.println("prosao sam "+id);	
						}
						synchronized (raskrsnica.kjugD) {
							raskrsnica.kjugD--;
							
						}
						voznjaPrekoUlice();
						synchronized (raskrsnica.kjugD) {
							//if (raskrsnica.kjugD == 0) raskrsnica.jugDsenz = false;
						}
						label.hide();
						break;
					case 32: if ((raskrsnica.getJugS()[1].dohvatiSvetlo() != 3) && (raskrsnica.getJugS()[1].dohvatiSvetlo() != 0)) {
						 raskrsnica.dodajAuto(this, 32);//TODO videti da li je ok br
						 raskrsnica.jugPsenz = true;
						 b.wait();
						 
						 System.out.println("prosao sam "+id);	
						
						}		
					synchronized (raskrsnica.kjugP) {
						raskrsnica.kjugP--;
						
					}
					 voznjaPrekoUlice();
					 synchronized (raskrsnica.kjugP) {
						// if (raskrsnica.kjugP == 0) raskrsnica.jugPsenz = false;
					 }
					 label.hide();
						break;
					case 33: if ((raskrsnica.getJugS()[0].dohvatiSvetlo() != 3) && (raskrsnica.getJugS()[0].dohvatiSvetlo() != 0)) {
						 raskrsnica.dodajAuto(this, 33);//TODO videti da li je ok br
						 raskrsnica.jugLsenz = true;
						 b.wait();
						 
						 System.out.println("prosao sam "+id);	
						
						}		
					synchronized (raskrsnica.kjugL) {
						raskrsnica.kjugL--;
						
					}
					 voznjaPrekoUlice();
					 synchronized (raskrsnica.kjugL) {
						// if (raskrsnica.kjugL == 0) raskrsnica.jugLsenz = false;
					 }
					 label.hide();
						break;
					case 41: if ((raskrsnica.getZapadS()[2].dohvatiSvetlo() != 3) && (raskrsnica.getZapadS()[2].dohvatiSvetlo() != 0)) {
						 raskrsnica.dodajAuto(this, 41);//TODO videti da li je ok br
						 raskrsnica.zapadDsenz = true;
						 b.wait();
						 
						 System.out.println("prosao sam "+id);	
						}
						synchronized (raskrsnica.kzapadD) {
							raskrsnica.kzapadD--;
							
						}
						voznjaPrekoUlice();
						synchronized (raskrsnica.kzapadD) {
						//	if (raskrsnica.kzapadD == 0) raskrsnica.zapadDsenz = false;
						}
						label.hide();
						break;
					case 42:if ((raskrsnica.getZapadS()[1].dohvatiSvetlo() != 3) && (raskrsnica.getZapadS()[1].dohvatiSvetlo() != 0)) {
						 raskrsnica.dodajAuto(this, 42);//TODO videti da li je ok br
						 raskrsnica.zapadPsenz = true;
						 b.wait();
						 
						 System.out.println("prosao sam "+id);	
						
					 }		
			synchronized (raskrsnica.kzapadP) {
				raskrsnica.kzapadP--;
				
			}
			 voznjaPrekoUlice();
			 synchronized (raskrsnica.kzapadP) {
				// if (raskrsnica.kzapadP == 0) raskrsnica.zapadPsenz = false;
			 }
			 label.hide(); 
						break;
					case 43: if ((raskrsnica.getZapadS()[0].dohvatiSvetlo() != 3) && (raskrsnica.getZapadS()[0].dohvatiSvetlo() != 3)) {
						 raskrsnica.dodajAuto(this, 43);//TODO videti da li je ok br
						 raskrsnica.zapadLsenz = true;
						 b.wait();
						 
						 System.out.println("prosao sam "+id);	
						
					 }		
			synchronized (raskrsnica.kzapadL) {
				raskrsnica.kzapadL--;
				
			}
			 voznjaPrekoUlice();
			 synchronized (raskrsnica.kzapadL) {
				 //if (raskrsnica.kzapadL == 0) raskrsnica.zapadLsenz = false;
			 }
			 label.hide(); 
						break;
				}
			}
			catch(InterruptedException e)
			{
				e.printStackTrace();
			};
		
		}
	}
	
	public int getId() {
		return id;
	}
}